<?php
/**
 * Nguoi tao: nghiat
 * Ngay tao: 09/11/2010
 * Y nghia: Class thu ly HS
 */	
class mtl_userController extends  Zend_Controller_Action {	
	public function init(){	
        $tempDirApp = Zend_Registry::get('conDirApp');
		$this->_dirApp = $tempDirApp->toArray();
		$this->view->dirApp = $tempDirApp->toArray();
		if (!$this->_request->isXmlHttpRequest()){
			//Cau hinh cho Zend_layout
			Zend_Layout::startMvc(array(
					'layoutPath' => $this->_dirApp['layout'],
					'layout' => 'index'			    
					));	
			//Load ca thanh phan cau vao trang layout (index.phtml)
			$response = $this->getResponse();
			//Ky tu dac biet phan tach giua cac phan tu
			$this->view->delimitor 	= "!~~!";	
			//Load cau hinh thu muc trong file config.ini de lay ca hang so dung chung
			$tempConstPublic = Zend_Registry::get('ConstPublic');
			$this->_ConstPublic = $tempConstPublic->toArray();	
			$objlibrary = new Efy_Library();
			//Lay duong dan thu muc goc (path directory root)
			$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";	
			
			//Goi lop Listxml_modList
			//Zend_Loader::loadClass('record_modTax');
			Zend_Loader::loadClass('mtl_modUser');
			//Zend_Loader::loadClass('record_modReceive');
			//Lay cac hang so su dung trong JS public
			$objConfig = new Efy_Init_Config();
			$this->view->JSPublicConst = $objConfig->_setJavaScriptPublicVariable();		
			
			//Tao doi tuong XML
			Zend_Loader::loadClass('Efy_Publib_Xml');		
			// Load tat ca cac file Js va Css
			$this->view->LoadAllFileJsCss = $objlibrary->_getAllFileJavaScriptCss('','efy-style','style_OnNet.css',',','css');	
					
			//Lay tra tri trong Cookie
			$sGetValueInCookie = $objlibrary->_getCookie("showHideMenu");
			
			//Neu chua ton tai thi khoi tao
			if ($sGetValueInCookie == "" || is_null($sGetValueInCookie) || !isset($sGetValueInCookie)){
				$objlibrary->_createCookie("showHideMenu",1);
				$objlibrary->_createCookie("ImageUrlPath",$this->_request->getBaseUrl() . "/public/images/close_left_menu.gif");
				//Mac dinh hien thi menu trai
				$this->view->hideDisplayMeneLeft = 1;// = 1 : hien thi menu
				//Hien thi anh dong menu trai
				$this->view->ShowHideimageUrlPath = $this->_request->getBaseUrl() . "/public/images/close_left_menu.gif";
			}else{//Da ton tai Cookie
				/*
				Lay gia tri trong Cookie, neu gia tri trong Cookie = 1 thi hien thi menu, truong hop = 0 thi an menu di
				*/
				if ($sGetValueInCookie != 0){
					$this->view->hideDisplayMeneLeft = 1;// = 1 : hien thi menu
				}else{
					$this->view->hideDisplayMeneLeft = "";// = "" : an menu
				}
				//Lay dia chi anh trong Cookie
				$this->view->ShowHideimageUrlPath = $objlibrary->_getCookie("ImageUrlPath");
			}
			$this->view->currentModulCode = "RECORD-NET";
			$response->insert('header', $this->view->renderLayout('header.phtml','./application/views/scripts/'));    	//Hien thi header 
			$response->insert('menu', $this->view->renderLayout('menu.phtml','./application/views/scripts/'));    		//Hien thi header 		
	        $response->insert('footer', $this->view->renderLayout('footer.phtml','./application/views/scripts/')); 
		}
	}
	/**
	 * Idea : Phuong thuc hien thi danh sach
	 */
	public function indexAction(){			
		//Goi cac doi tuong		
		$md5_hash = md5(rand(0,999)); 
		$security_code = substr($md5_hash, 15, 9); 
		$_SESSION["security_code"] = $security_code;
		//main or support
	}
	/**
	 * Idea : changepass
	 */
	public function changepassAction(){			
		$objUser		= new mtl_modUser();		
		$sOldpass 		= 	$this->_request->getParam('hdn_oldpass',"");
		$sNewpass 		= 	$this->_request->getParam('hdn_newpass',"");		
		if(isset($sOldpass)& $sOldpass!=""){
			$sOldpass=md5($sOldpass);	echo $sOldpass;	
			$login=$objUser->eCSNetCheckLogin($_SESSION["user_name1"],$sOldpass);var_dump($login);//exit;
			if(is_null($login) || sizeof($login)==0 ||!$login){?>
				<script language="JavaScript">
					alert('Mật khẩu không đúng');
				</script>				
			<?php }
			else {
				$changepass=$objUser->eCSNetUpdatePass($_SESSION["user_name1"],md5($sNewpass));?>
				<script language="JavaScript">
					alert('Cập nhật thành công');
				</script>			 
			<?php	$this->_redirect('record/sendrecord/worklogin/?username='.$_SESSION["user_name1"].'&password='.$sNewpass);
			}
		}
	}
	/**
	 * 
	 * Enter description here ...
	 */
	public function addAction(){ //Cap nhat chuyen tiep HS
		$pUpdate = $this->_request->getParam('hdn_update','');
		$md5_hash = md5(rand(0,999)); 
		//Goi doi tuong
		$objRecordFunction	     = new Efy_Function_RecordFunctions();	
		$objTax	  				 = new mtl_modUser();
		$objInitConfig 			 = new Efy_Init_Config();
		$ojbEfyLib				 = new Efy_Library();
		$sFullname 				 = 	$this->_request->getParam('txt_fullname',"");
		$sUsername 				 = 	$this->_request->getParam('txt_username',"");
		$sPassword 				 = 	$this->_request->getParam('txt_password',"");
		$sRePassword 			 = 	$this->_request->getParam('txt_repassword',"");
		$sEmail		 			 = 	$this->_request->getParam('txt_email',"");
		$sSocmnd 				 = 	$this->_request->getParam('txt_scmnd',"");
		$this->view->fullname	=$sFullname;
		$this->view->username	=$sUsername;
		$this->view->password	=$sPassword;
		$this->view->repassword	=$sRePassword;
		$this->view->email		=$sEmail;
		$this->view->socmnd		=$sSocmnd;
		if($pUpdate=='1'){
			//Cap nhat CSDL
			$arrParameter = array(	
						'PK_NET_ID'									=>	'',															
						'C_FULLNAME'								=>	$sFullname,
						'C_USERNAME'								=>	$sUsername,															
						'C_PASSWORD'								=>	md5($sPassword),
						'C_EMAIL'									=>	$sEmail,
						'C_ID_CARD'									=>	$sSocmnd,
						'C_CREATED_DATE'							=>	null,	 
						'C_XML_DATA'								=>	null							
						);
			$arrResult = $objTax->eCSNetUserUpdate($arrParameter);	//Goi model cap nhat vao CSDL
			if(is_null($arrResult['RET_ERROR'])||$arrResult['RET_ERROR']=='' ){
				$this->_redirect('mtl/search/training/');
			}else{?>
				<script language="JavaScript">
					alert('<? echo $arrResult['RET_ERROR']?>');
				</script>
			<?php }
		}
	}
	/**
	 * 
	 * Enter description here ...
	 */
	public function getcaptchaAction(){
		Zend_Loader::loadClass('Efy_Captcha');
		$objConfig = new Efy_Init_Config();
		$sImgDir = $_SERVER['DOCUMENT_ROOT'].$objConfig->_setWebSitePath().'public/captcha/';
		$sImgUrl = $objConfig->_setWebSitePath().'public/captcha/';
		$captcha = new Efy_Captcha();
		$captcha->setImgDir($sImgDir);
		$captcha->setImgUrl($sImgUrl);
		$captcha->setFont($sImgDir.'League_Gothic-webfont.ttf');
		$captcha->setWordlen(6);
		$captcha->setFontSize(28);
		$captcha->setLineNoiseLevel(3);
		$captcha->setWidth(120);
		$captcha->setHeight(64);
		//$captcha->setGcFreq(5);
		$captcha->generate();
		Zend_Loader::loadClass('Zend_Session_Namespace');
		$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captcha->getId());
		$captchaWord = $captchaSession->word;
		$_SESSION["security_code"] = $captchaWord;
		$this->view->captcha = $captcha;
	}
	public function checkcaptchaAction(){
		//Kiem tra captcha
		$captcha = $_REQUEST['v_input'];
		$arrCaptcha = array(	
							'id'	=>	$_SESSION["security_code"],															
							'input'	=>	$captcha							
							);
		$iCheck = $this->validateCaptcha($arrCaptcha);
		echo $iCheck;exit;
	}
	//validates captcha response
	private function validateCaptcha($captcha) {
		$captchaId = $captcha['id'];
		$captchaInput = $captcha['input'];
		//echo $captchaId,'-',$captchaInput;
		if( $captchaInput != $captchaId ){
			return 0;
		} else {
			return 1;
		}
	}
	/**
	 * Idea : Phuong thuc dang nhap
	 *
	 */
	public function loginAction(){
	    $objrecordfun 				= new Efy_Function_RecordFunctions();
		$objInitConfig 			 	= new Efy_Init_Config();
		$objXml					 	= new Efy_Publib_Xml();
        $objLibrary 				= new Efy_Library();
		// Tieu de tim kiem
		$this->view->bodyTitleSearch = "TRA CỨU HỒ SƠ TTHC QUA MẠNG";				
		// Tieu de man hinh danh sach
		$this->view->bodyTitle = "TRA CỨU HỒ SƠ TTHC QUA MẠNG";
        $objConfig  = new  Efy_Init_Config();
        $this->view->url = 'http://'.$_SERVER['HTTP_HOST'].$objConfig->_setWebSitePath();
		//thong bao khi dang nhap sai 
		$sUsername = $this->_request->getParam('hdh_username','');
		$sMessage = $this->_request->getParam('hdn_message','');	
		$sRecordTypeID = $this->_request->getParam('recordType','');
		$sName = $this->_request->getParam('hdn_register_name','');
		$sCode = $this->_request->getParam('hdn_code','');	
		//$this->view->message=	$sMessage;	//echo $sMessage;	
		
		$this->view->name=$sName;
		$this->view->code=$sCode;
        /**
         * --------- Lu mat khau vao cookies ----------
         */
        $sUserName = '';
        $sPassWord = '';
        $sCheckReme = $objLibrary->_getCookie("sCheckReme_net");
        if($sCheckReme){
		   $sUsername = $objLibrary->_getCookie("sUserName_net");
		   $sPassWord = $objLibrary->_getCookie("sPassWord_net");
		}else{
		  $sCheckReme = 0;
		}
        $this->view->username=$sUsername;
        $this->view->password=$sPassWord;
        $this->view->sCheckReme= $sCheckReme;
		if($sMessage){?>
			<script language="JavaScript">
					alert('Đăng nhập thất bại!');
			</script>
		<?php }
        if(isset($_SESSION['user_name_net']) && $_SESSION['user_name_net'] !==''){
            $this->_redirect('/mtl/search/training/');
        }
	}
	/**
	 *
	 *
	 */
	public function workloginAction(){
		$objrecordfun 			= new Efy_Function_RecordFunctions();
		$objInitConfig 			= new Efy_Init_Config();
		$objXml					= new Efy_Publib_Xml();
		$objRecordArchive 		= new mtl_modUser();
        $objLibrary 		    = new Efy_Library();
        // Tieu de tim kiem
		$this->view->bodyTitleSearch = "THAO TÁC NGƯỜI DÙNG";				
		// Tieu de man hinh danh sach
		$this->view->bodyTitle = "THAO TÁC NGƯỜI DÙNG";
		$response = $this->getResponse();
		$sUsername = $this->_request->getParam('txt_usename','');
		if($sUsername=='')	{
			$sUsername = $this->_request->getParam('username','');
		}
		$this->view->username=$sUsername;
		$sPassword = $this->_request->getParam('txt_password','');
		if($sPassword==''){
			$sPassword = $this->_request->getParam('password','');
		}
        if ($sPassword!=''){
			$sPasswordMd5=md5($sPassword)	;
		}
		$arrCheckLogin=$objRecordArchive->eCSNetCheckLogin($sUsername,$sPasswordMd5);	//var_dump($arrCheckLogin);exit;
		//neu dang nhap sai
		if(is_null( $arrCheckLogin)||sizeof( $arrCheckLogin)==0){
			 $this->_redirect('/mtl/user/login/?hdh_username='.$sUsername."&hdn_message=1");	
		}
		else {
		     $sCheckReme = $this->_request->getParam('hdn_autorem');
             $objLibrary->_createCookie("sCheckReme_net",$sCheckReme);
             if($sCheckReme){
    			$objLibrary->_createCookie("sUserName_net",$sUsername);
    			$objLibrary->_createCookie("sPassWord_net",$sPassword);
    		 }
			$_SESSION['user_id_net'] =$arrCheckLogin['PK_NET_ID'];
            $_SESSION['full_name_net'] = $arrCheckLogin['C_FULLNAME'];
            $_SESSION['user_name_net'] = $arrCheckLogin['C_USERNAME'];
			$this->_redirect('/mtl/search/training/');
		}
		exit;
	}
	/**
	*
	*
	**/
	public function logoutAction(){			
		
	}
}?>
